package com.cws.job;

import java.util.Iterator;
import java.util.Set;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cws.enmu.ConfigKey;
import com.cws.sys.Mem;

/**
 * @title [定时任务]
 * @description [token有效性检查]
 * @copyright Copyright 2013 SHIPING INFO Corporation. All rights reserved.
 * @company SHIPING INFO.
 * @author Caspar Du
 * @version v 1.0
 * @create 2013-6-9 下午10:34:47
 */
public class TokenAudit implements Job {

	private static final Logger log = LoggerFactory.getLogger(TokenAudit.class);

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		//log.debug("Token Audit start");
		
		int tokenInvalidTime = Integer.parseInt(Mem.getConfig().getProperty(ConfigKey.tokenInvalidTime.name()));

		Set<String> set = Mem.getTokenInfo().keySet();
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			String tokenId = (String) it.next();
			Long lastTime = Mem.getTokenInfo().get(tokenId);
			if ((System.currentTimeMillis() - lastTime) >= (tokenInvalidTime * 60 * 1000)) {
				log.info("token audit thread remove token:" + tokenId);
				Mem.removeToken(tokenId);
			}
		}
		
		//log.debug("Token Audit end");
	}

}
